﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Threading;
using Wormhole.Engine.Player;
using UITheme = Wormhole.UI.Application.Theme.Theme;

namespace Wormhole.UI.Application.Controls.Power
{
	/// <author>Rodney McKay</author>
	/// <date>02 April 2008</date>
	/// <summary>
	/// This controls displays the player power.
	/// </summary>
	public partial class PlayerPower : UserControl
	{
		#region Properties

		#region Player
		private Wormhole.Engine.Player.Player _player;
		/// <summary>
		/// The game engine player object.
		/// </summary>
		public Wormhole.Engine.Player.Player Player 
		{
			get
			{
				return this._player;
			}

			set
			{
				if (this._player != value)
				{
					this._player = value;
					this.SetEngineEvents();
				}
			}
		}
		#endregion

		#region Player Type
		private PlayerType _playerType;
		/// <summary>
		/// The player type.
		/// </summary>
		public PlayerType PlayerType 
		{
			get
			{
				return this._playerType;
			}

			set
			{
				this._playerType = value;

				// apply the theme
				if (this.PlayerType == PlayerType.Hero)
					this.ApplyTheme(UITheme.Power.HeroPower);
				else if (this.PlayerType == PlayerType.Villian)
					this.ApplyTheme(UITheme.Power.VillianPower);
			}
		}
		#endregion

		#endregion

		#region Constructor
		/// <summary>
		/// Initialize a default instance of the class.
		/// </summary>
		public PlayerPower()
		{
			InitializeComponent();
		}
		#endregion

		#region Set Engine Events
		protected void SetEngineEvents()
		{
			this.Player.PowerChanged += new Player.PowerChangedDelegate(Power_PowerChanged);
			this.DisplayPower(this.Player.Power);
		}
		#endregion

		#region Apply Theme
		/// <summary>
		/// Apply the player theme.
		/// </summary>
		/// <param name="theme">The theme to be applied.</param>
		protected void ApplyTheme(UITheme.Power.IPlayerPower theme)
		{
			this.gsColor.Color = theme.GradientColor;
			this.imgIcon.Source = new BitmapImage(new Uri(theme.IconUrl, UriKind.Relative));
		}
		#endregion

		#region Event Handlers

		#region Power Changed
		/// <summary>
		/// Power Changed
		/// </summary>
		/// <param name="power">The new power value.</param>
		void Power_PowerChanged(int power)
		{
			this.Dispatcher.Invoke(
				DispatcherPriority.Normal,
				(System.Windows.Forms.MethodInvoker)delegate()
			{
				this.DisplayPower(power);
			});
		}
		#endregion	

		#region Mouse Down
		/// <summary>
		/// Handles mouse clicks on the control.
		/// </summary>
		protected override void OnMouseDown(MouseButtonEventArgs e)
		{
			if (e.LeftButton == MouseButtonState.Pressed)
				this.IncreasePower();
			else if (e.RightButton == MouseButtonState.Pressed)
				this.DecreasePower();
		}
		#endregion

		#endregion

		#region Display Power
		/// <summary>
		/// Display the player power.
		/// </summary>
		/// <param name="power">The power to display.</param>
		protected void DisplayPower(int power)
		{
			this.lblPower.Content = power;
		}
		#endregion

		#region Increase Power
		/// <summary>
		/// Increase the player power.
		/// </summary>
		protected void IncreasePower()
		{
			this.Player.Power++;
		}
		#endregion

		#region Decrease Power
		/// <summary>
		/// Decrease the player power.
		/// </summary>
		protected void DecreasePower()
		{
			this.Player.Power--;
		}
		#endregion
	}
}
